//-----------------------------------------------------------------------
// <copyright file="CreateVirtualDirectory.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Creates a new Virtual Directory.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Web.FtpSite
{
    using System;
    using System.Xml;
    using System.Globalization;
    using Microsoft.Sdc.Tasks.Configuration.Web;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Create a new ftp virtual directory if it does not exist on the specified machine and ftp site.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.FtpSite.CreateVirtualDirectory 
    ///         VirtualDirectoryName="virtualDirectoryName" 
    ///         Path="path"
    ///         AccessFlags="accessFlags"
    ///         FtpSiteName="ftpSiteName"
    ///         MachineName="machineName"
    /// >]]></code>
    /// <para>where:</para>
    /// <para><i>virtualDirectoryName (Required)</i></para>
    /// <para>A valid virtual directory name.</para>
    /// <para><i>path (Required)</i></para>
    /// <para>Fully qualified path to assign to the virtual directory.</para>
    /// <para><i>machineName</i></para>
    /// <para>Machine to create the virtual directory on. If unspecified the action will execute against the local machine.</para>
    /// <para><i>ftpSiteName</i></para>
    /// <para>The FTP site to attach the virtual directory to.</para>
    /// <para><i>accessFlags</i></para>
    /// <para>Access flags for the FTP site (either "AccessRead", "AccessWrite", or both combined)</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.FtpSite.CreateVirtualDirectory
    ///             VirtualDirectoryName="/virtual"
    ///             Path="c:\inetpub\virtual"
    ///             AccessFlags="AccessRead"
    ///        />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class CreateVirtualDirectory : TaskBase
    {
        private string machineName = String.Empty;
        private string ftpSiteName = String.Empty;
        private string name = String.Empty;
        private string path = String.Empty;
        private string accessFlagsFromConfig = String.Empty;
        private AccessFlags accessFlags = 0;

        /// <summary>
        /// Initializes a new instance of the CreateFtpVirtualDirectory class.
        /// </summary>
        public CreateVirtualDirectory()
        {
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code

            FtpSite ftp;
            FtpVirtualDirectory vdir;

            if (ftpSiteName.Length == 0)
            {
                ftpSiteName = "Default FTP Site";
            }

            if (machineName.Length == 0)
            {
                machineName = "localhost";
            }

            if (accessFlagsFromConfig == null || accessFlagsFromConfig.Length == 0)
            {
                accessFlags = Microsoft.Sdc.Tasks.Configuration.Web.AccessFlags.AccessWrite |
                              Microsoft.Sdc.Tasks.Configuration.Web.AccessFlags.AccessRead;
            }
            else
            {
                string[] accessFlagsSplit = accessFlagsFromConfig.Split(' ');
                foreach (string accessFlagsSplitItem in accessFlagsSplit)
                {
                    object flags = Enum.Parse(typeof(AccessFlags), accessFlagsSplitItem, true);
                    accessFlags = accessFlags | (AccessFlags) flags;
                }
            }

            ftp = FtpSite.Load(machineName, ftpSiteName);
            //if it exists do nothing
            if (ftp.VirtualDirectories[name] == null)
            {
                Log.LogMessageFromResources("Web.CreateVirtualDirectory", name);
                vdir = new FtpVirtualDirectory(name);

                // TODO read these from xml or default
                //
                vdir.AccessFlags = accessFlags;
                vdir.Path = System.IO.Path.GetFullPath(path);
                ftp.VirtualDirectories.Add(vdir);
                ftp.Save();
            }
            else
            {
                Log.LogMessageFromResources(MessageImportance.Low, "Web.VirtualDirectoryExists", name);
            }
            #endregion
        }

        /// <summary>
        /// Name for the new virtual directory
        /// </summary>
        /// <value>any valid directory name</value>
        [Required]
        public string VirtualDirectoryName
        {
            get
            {
                return (name == null ? String.Empty : name);
            }
            set
            {
                name = value;
            }
        }

        /// <summary>
        /// Local, or network path to be mapped to the virtual directory
        /// </summary>
        /// <value>A valid local or network path</value>
        [Required]
        public string Path
        {
            get
            {
                return (path == null ? String.Empty : path);
            }
            set
            {
                path = value;
            }
        }

        /// <summary>
        /// Machine name to create the virtual directory on
        /// </summary>
        /// <value>A valid machine name. 
        /// If unspecified the action will execute against the local machine</value>
        public string MachineName
        {
            get
            {
                return (machineName == null ? String.Empty : machineName);
            }
            set
            {
                machineName = value;
            }
        }

        /// <summary>
        /// FTP site name to attach the virtual directory to
        /// </summary>
        /// <value>A valid, existing FTP site name</value>
        public string FtpSiteName
        {
            get
            {
                return (ftpSiteName == null ? String.Empty : ftpSiteName);
            }
            set
            {
                ftpSiteName = value;
            }
        }

        /// <summary>
        /// Access flags for the new virtual directory
        /// </summary>
        /// <value>"AccessRead" or "AccessWrite" (may be combined)</value>
        public string AccessFlags
        {
            get
            {
                return (accessFlagsFromConfig == null ? String.Empty : accessFlagsFromConfig);
            }
            set
            {
                accessFlagsFromConfig = value;
            }
        }
    }
}

